import { inject, injectable } from 'inversify';
import { RoleListModel } from '../models/role.list.model.ts';
import { UsersRolesRepository } from '../data/users.roles.repository.ts';
import { requestWOMeta } from '../data/user.roles.dto.ts';
import { roleDetailMapper } from '../mappers/role.list.mapper.ts';

@injectable()
export class CreateRoleUseCase {
  constructor(
    @inject(RoleListModel) private roleListModel: RoleListModel,
    @inject(UsersRolesRepository) private rolesRepository: UsersRolesRepository,
  ) {}

  async execute(params: requestWOMeta) {
    const newRole = roleDetailMapper(params);
    try {
      this.roleListModel.addRole(newRole);

      await this.rolesRepository.createRole(params);
    } catch (e) {
      this.roleListModel.removeRole(newRole._id);
    }
  }
}
